﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBControl;
using System.Web.Security;

namespace ProjectX
{
    public partial class regedit : Errorbase
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void regeditbtn_Click(object sender, ImageClickEventArgs e)
        {
            if (this.usernameCustomValidator.IsValid)
            {
                SqlDbHelper sdh = new SqlDbHelper();
                string ueserPwssword = FormsAuthentication.HashPasswordForStoringInConfigFile(this.pwdtxt.Text, "MD5");
                string register = "INSERT INTO UserRegister(userName,passWord,email) VALUES('" + this.username.Text + "','" + ueserPwssword + "','"+ this.email.Text +"')";
                try
                {
                    sdh.ExecuteNonQuery(register);
                    Session.Add("userNick", this.username.Text);
                    Response.Redirect("/", false);
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                    throw;
                }
            }
            
        }

        private void usernamecheck()
        { 
            
        }

        protected void usernameCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
        {
            string getusername = "SELECT userName FROM UserRegister WHERE userName = '"+ args.Value +"'";
            SqlDbHelper sdh = new SqlDbHelper();
            if (sdh.ExecuteDataTable(getusername).Rows.Count > 0)
            {
                args.IsValid = false;
            }
            else
            {
                args.IsValid = true;
            }
        }

        protected void agree_CheckedChanged(object sender, EventArgs e)
        {
            if (this.agree.Checked)
            {
                this.regeditbtn.ImageUrl = "~/Webimg/regBtn.png";
                this.regeditbtn.Enabled = true;
            }
            else
            {
                this.regeditbtn.ImageUrl = "~/Webimg/regBtnEnable.png";
                this.regeditbtn.Enabled = false;
            }
        }
    }
}